using UnityEngine;
using QFramework;
namespace PlatformShoot
{
    public class Player : PlatformShootGameController
    {
        private Rigidbody2D mRig;
        private BoxCollider2D mBoxColl;
        private LayerMask mGroundLayer;
        private float mAccDelta = 0.6f;
        private float mDecDelta = 0.9f;
        private float mGroundMoveSpeed = 5f;
        private float mJumpFirce = 12f;
        private bool mJumpInput;
        private bool isJumping;
        int myFaceDir = 1;
        int mInputX;
        int mInputY;
        bool mAttackInput;
        bool mGround;
        private IObjectPoolSystem objectPool;
        private IAudioMgrSystem audioMgr;
        void Awake()
        {
            this.SendCommand<InitGameCommand>();
        }
        private void Start()
        {
            mRig = GetComponent<Rigidbody2D>();
            mBoxColl = GetComponentInChildren<BoxCollider2D>();
            mGroundLayer = LayerMask.GetMask("Ground");
            this.GetSystem<ICameraSystem>().SetTarget(this.transform);
            objectPool = this.GetSystem<IObjectPoolSystem>();
            audioMgr = this.GetSystem<IAudioMgrSystem>();
            this.RegisterEvent<DirInputEvent>(e =>
            {
                mInputX = e.x;
                mInputY = e.y;
            });
            this.RegisterEvent<ShootInputEvent>(e =>
            {
                mAttackInput = e.isTrigger;

            });
            this.RegisterEvent<JumpInputEvent>(e =>
            {
                if (mGround)
                {
                    audioMgr.PlaySound("跳跃");
                    mJumpInput = true;
                    isJumping = true;
                }
            });
            audioMgr.PlayBgm("黑色之翼");
        }
        void Update()
        {
            if (mAttackInput)
            {
                mAttackInput = false;
                audioMgr.PlaySound("竖琴");
                objectPool.Get("Item/Bullet", o =>
                {
                    o.transform.localPosition = transform.position;
                    o.GetComponent<Bullet>().InitDir(myFaceDir);
                });
            }
            mGround = Physics2D.OverlapBox(transform.position + mBoxColl.size.y * Vector3.down * 0.5f, Vector3.one * 0.1f, 0, mGroundLayer);
            if (mGround && isJumping)
            {
                audioMgr.PlaySound("落地");
                isJumping = false;
            }
        }
        private void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.red;
            if (mBoxColl == null) return;
            Gizmos.DrawWireCube(transform.position + mBoxColl.size.y * Vector3.down * 0.5f, new Vector2(mBoxColl.size.x * 0.9f, 0.1f));
        }
        private void FixedUpdate()
        {
            if (mJumpInput)
            {
                mJumpInput = false;
                mRig.velocity = new Vector2(mRig.velocity.x, mJumpFirce);
            }
            if (mInputX != 0)
            {
                mRig.velocity = new Vector2(Mathf.Clamp(mRig.velocity.x + mInputX * mAccDelta, -mGroundMoveSpeed, mGroundMoveSpeed), mRig.velocity.y);
            }
            else
            {
                mRig.velocity = new Vector2(Mathf.MoveTowards(mRig.velocity.x, 0, mDecDelta), mRig.velocity.y);
            }
            Flip(mInputX);
            //mRig.velocity = new Vector2(h * mGroundMoveSpeed, mRig.velocity.y);
        }

        private void Flip(float h)
        {
            if (h != 0 && h != myFaceDir)
            {
                myFaceDir = -myFaceDir;
                transform.Rotate(0, 180, 0);
            }
        }

        // void LateUpdate()
        // {
        //     this.GetSystem<ICameraSystem>().Update();
        // }
        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.gameObject.CompareTag("Reward"))
            {
                GameObject.Destroy(collision.gameObject);
                this.GetModel<IGameModel>().Score.Value++;
                audioMgr.PlaySound("拾取金币");
            }
            if (collision.gameObject.CompareTag("Door"))
            {
                this.SendCommand(new NextLevelCommand("GamePassScene"));
                audioMgr.PlaySound("通关音效");
            }
        }
    }
}